﻿using System;
using System.Net.Mail;

public partial class Contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Page.Title = "Liên hệ khách hàng";
    }
    protected void btnSumit_Click(object sender, EventArgs e)
    {
        SendMail();        
    }

    private void SendMail()
    {
        SmtpClient SmtpServer = new SmtpClient
                                    {
                                        Credentials =
                                            new System.Net.NetworkCredential("ishop1805@gmail.com", "iShopcontact"),
                                        Port = 587,
                                        Host = "smtp.gmail.com",
                                        EnableSsl = true
                                    };
        MailMessage mail = new MailMessage();
        try
        {

            mail.From = new MailAddress(txtEmail.Text, txtHoTen.Text + " - iShop", System.Text.Encoding.UTF8);
            mail.To.Add("ishop1805@gmail.com");
            mail.Subject = "Mail từ iShop Contact Form";
            mail.Body = MailBody();
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            mail.ReplyTo = new MailAddress("ishop1805@gmail.com");
            mail.Priority = MailPriority.High;
            mail.IsBodyHtml = true;
            SmtpServer.Send(mail);
            lblThongBao.Text = "OK";
        }
        catch (Exception ex)
        {
            lblThongBao.Text = "Lỗi: "+ex.Message;
            //throw;
        }
    }

    private string MailBody()
    {
        var strHTML = "";
        strHTML += String.Format("Họ tên: {0}<br/>", txtHoTen.Text);
        strHTML += String.Format("Số điện thoại: {0}<br/>", txtSoDienThoai.Text);
        strHTML += String.Format("Email: {0}<br/>", txtEmail .Text);
        strHTML += "Đã gửi qua Form liên hệ với thông điệp:<br><p><b>";
        strHTML += String.Format("Nội dung: {0}</b></p>", txtYKien.Text);
        return strHTML;

    }
}
